package com.lhkj.ct.admin.modules.gauge.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblGaugeUsability;
import com.lhkj.ct.meta.modules.gauge.service.GaugeUsabilityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 适用人群标签 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-10-08
 */
@RestController
@RequestMapping("api/plat/gauge/usability")
@Validated
@Api(tags = "6.5适用人群分类")
public class GaugeUsabilityController {

    @Resource
    private GaugeUsabilityService usabilityService;

    @GetMapping(value = "list")
    @ApiOperation(value = "列表接口")
    @ApiOperationSupport(order = 1)
    public ActionResult<IPage<TblGaugeUsability>> list(TblGaugeUsability usability, PageQuery pageQuery) {
        return ActionResult.success(usabilityService.selectUsabilityPage(usability, pageQuery));
    }

    @GetMapping(value = "options")
    @ApiOperation(value = "下拉选项分页接口")
    public ActionResult<IPage<Option<String>>> selectCategoryOptions(@Validated(ValGroups.Get.class) TblGaugeUsability usability, PageQuery pageQuery) {
        return ActionResult.success( usabilityService.selectUsabilityOptions(usability, pageQuery));
    }

    @GetMapping(value = "checkName")
    @ApiOperation(value = "校验名称是否唯一")
    @ApiOperationSupport(order = 2)
    public ActionResult<Boolean> checkName(TblGaugeUsability usability) {
        return ActionResult.success(usabilityService.checkNameUnique(usability));
    }

    @GetMapping(value = "{cId}/get")
    @ApiOperation(value = "表单数据")
    @ApiOperationSupport(order = 3)
    public ActionResult<TblGaugeUsability> get(@PathVariable String cId) {
        return ActionResult.success(usabilityService.getById(cId));
    }

    @PreventDuplicateSubmit
    @PostMapping(value = "add")
    @ApiOperation(value = "新增")
    @ApiOperationSupport(order = 4)
    @Log(title = "适用人群", actionType = ActionType.INSERT)
    public ActionResult<TblGaugeUsability> add(@Validated(ValGroups.Insert.class) @RequestBody TblGaugeUsability usability) {
        Asserts.assertTrue(usabilityService.checkNameUnique(usability), "标签名称已存在");
        usabilityService.insert(usability);
        return ActionResult.success(usability);
    }

    @PutMapping(value = "update")
    @ApiOperation(value = "修改")
    @ApiOperationSupport(order = 5)
    @Log(title = "适用人群", actionType = ActionType.UPDATE)
    public ActionResult<Void> update(@Validated({ValGroups.Update.class}) @RequestBody TblGaugeUsability usability) {
        TblGaugeUsability target = usabilityService.getById(usability.getId());
        Asserts.assertFalse(null == target, "标签不存在");
        Asserts.assertTrue(usabilityService.checkNameUnique(usability), "标签名称已存在");
        return ActionResult.judge(usabilityService.updateById(usability));
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除", notes = "支持批量删除")
    @ApiOperationSupport(order = 6)
    @Log(title = "适用人群", actionType = ActionType.DELETE)
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "删除的编号不能为空") List<String> ids) {
        return ActionResult.success("删除完成，共删除【" + usabilityService.removeByIds(ids) + "】条");
    }

}
